using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using EntityLayer;

public partial class Admin_Photos : SFPage
{
    busPhotos insBusPhotos = new busPhotos();
    entPhotos insEntPhotos = new entPhotos();
    protected void Page_Load(object sender, EventArgs e)
    {
        ibtUploadFile.Attributes.Add("onclick","OpenPictureUpload('" + txtPhotoPath.ClientID + "','" + txtPhotoDescription.ClientID +"');return false;");
        //if (!Page.IsPostBack)
        //{
        //    if (sfdPhotoAlbum.LoadComboBoxData())
        //    {
        //        return;
        //    }
        //}
    }
    protected bool sfgPhotos_NeedsDataSource()
    {
        DataTable insDataTablePhotos = insBusPhotos.SelectPhotos();
        if (insBusPhotos.HasErrors)
        {
            MessageBoxShow(insBusPhotos);
        }
        sfgPhotos.SetData(insDataTablePhotos);
        return insBusPhotos.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgPhotos_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntPhotos.PhotoId = Convert.ToInt32(e.CommandArgument);
            insBusPhotos.DeletePhotosByPhotoId(insEntPhotos);
            if (insBusPhotos.HasErrors)
            {
                MessageBoxShow(insBusPhotos);
                return;
            }
            sfgPhotos.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstPhotos.LoadDataListData();

    }
    protected bool lstPhotos_NeedsDataSource()
    {
        insEntPhotos.PhotoId = this.Id;
        insBusPhotos.SelectPhotosByPhotoId(insEntPhotos);
        if (insBusPhotos.HasErrors)
        {
            MessageBoxShow(insBusPhotos);
            return true;
        }
        txtPhotoDescription.Text = insEntPhotos.PhotoDescription;
        txtPhotoPath.Text = insEntPhotos.PhotoPath;
        calDatePictureTaken.Value = insEntPhotos.DatePictureTaken;
        if (this.Id == 0)
        {
            sfdPhotoAlbum.Value = null;
        }
        else
        {
            sfdPhotoAlbum.Value = insEntPhotos.PhotoAlbumId;
        }

        entPhotoContents insEntPhotoContents = new entPhotoContents();
        insEntPhotoContents.PhotoId = insEntPhotos.PhotoId;
        busPhotoContents insBusPhotoContents = new busPhotoContents();
        DataTable insDataTablePhotoContents = insBusPhotoContents.SelectPhotoContentsByPhotoId(insEntPhotoContents);
        if (insBusPhotoContents.HasErrors)
        {
            MessageBoxShow(insBusPhotoContents);
            return true;
        }

        

        lstPhotos.SetData(insDataTablePhotoContents);
        return false;
    }

    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }

        insEntPhotos.PhotoDescription = txtPhotoDescription.Text;
        insEntPhotos.PhotoPath = txtPhotoPath.Text;
        insEntPhotos.PhotoAlbumId = sfdPhotoAlbum.Value.Value;
        insEntPhotos.DatePictureTaken = calDatePictureTaken.Value.Value;

        foreach (DataListItem dli in lstPhotos.Items)
        {
            entPhotoContents insEntPhotoContents = new entPhotoContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntPhotoContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntPhotoContents.PhotoName = ((SFTextBox)dli.FindControl("txtPhotoName")).Text;
                insEntPhotoContents.PhotoDescription = ((SFTextBox)dli.FindControl("txtPhotoDescription")).Text;
                if (lstPhotos.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntPhotoContents.PhotoContentId = Convert.ToInt32(lstPhotos.DataKeys[dli.ItemIndex]);
                    insEntPhotos.PhotoContentsUpdate.Add(insEntPhotoContents);
                }
                else
                {
                    insEntPhotos.PhotoContentsInsert.Add(insEntPhotoContents);
                }
            }
            else
            {
                if (lstPhotos.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntPhotoContents.PhotoContentId = Convert.ToInt32(lstPhotos.DataKeys[dli.ItemIndex]);
                    insEntPhotos.PhotoContentsDelete.Add(insEntPhotoContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusPhotos.InsertPhotos(insEntPhotos);
        if (insBusPhotos.HasErrors)
        {
            MessageBoxShow(insBusPhotos);
            return;
        }
        base.SaveNew();
        sfgPhotos.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntPhotos.PhotoId = this.Id;
        insBusPhotos.UpdatePhotosByPhotoId(insEntPhotos);
        if (insBusPhotos.HasErrors)
        {
            MessageBoxShow(insBusPhotos);
            return;
        }
        base.SaveExisting();
        sfgPhotos.LoadGridData();
    }

    protected void lstPhotos_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["PhotoContentId"] != DBNull.Value);
                e.Item.FindControl("pnlPhotoContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }
    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlPhotoContents").Visible = ((SFCheckBox)sender).Checked;
    }
    protected bool sfdPhotoAlbum_NeedsDataSource()
    {
        busPhotoAlbumContents insBusPhotoAlbumContents = new busPhotoAlbumContents();
        DataTable insDataTablePhotoAlbumContents =insBusPhotoAlbumContents.SelectPhotoAlbumContentsByLanguageId();
        if (insBusPhotoAlbumContents.HasErrors)
        {
            MessageBoxShow(insBusPhotoAlbumContents);
            return true;
        }
        sfdPhotoAlbum.SetData(insDataTablePhotoAlbumContents, "PhotoAlbumName", "PhotoAlbumId");
        return false;
    }
}

